<template>
  <div class="main_box">
    <el-row>
      <el-col :span="12">
        <Crumbs></Crumbs>
      </el-col>
      <!--<el-col :span="12">
        <div class="opration_wrap">
          <el-button type="success" icon="fa fa-plus" size="small" @click="handleClickAdd()">新增</el-button>
          <el-button type="success" icon="el-icon-refresh-right" @click="reload">刷新</el-button>
        </div>
      </el-col>-->
    </el-row>
    <el-row>
      <div class="search_wrap">
        <el-form :inline="true" :model="searchForm" ref="searchForm">
          <!--<el-form-item prop="customerNickName">
            <el-input v-model="searchForm.customerNickName" :clearable="true" placeholder="昵称"></el-input>
          </el-form-item>-->
          <el-form-item prop="tel">
            <el-input v-model="searchForm.tel" :clearable="true" placeholder="客户手机"></el-input>
          </el-form-item>
          <el-form-item prop="adviceType">
            <el-select v-model="searchForm.adviceType" :clearable="true" placeholder="请选择类型">
              <el-option key="1" label="建议" value="1"></el-option>
              <el-option key="2" label="评价" value="2"></el-option>
              <el-option key="3" label="咨询" value="3"></el-option>
              <el-option key="4" label="投诉/举报" value="4"></el-option>
              <el-option key="5" label="其他" value="5"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="state">
            <el-select v-model="searchForm.state" :clearable="true" placeholder="请选择状态">
              <el-option key="0" label="待处理" value="0"></el-option>
              <el-option key="1" label="已处理" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-row>
    <el-row ref="tableRow">
      <el-table :data="tableList" :fit="true">
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="customerNickName" label="客户昵称"></el-table-column>
        <el-table-column prop="tel" label="客户手机"></el-table-column>
        <el-table-column prop="adviceType" label="建议类型" :formatter="formatAdviceType"></el-table-column>
        <el-table-column prop="content" label="建议内容"></el-table-column>
        <el-table-column prop="phone" label="联系方式"></el-table-column>
        <el-table-column prop="createDate" label="创建时间"></el-table-column>
        <el-table-column prop="state" label="状态" :formatter="formatStatue"></el-table-column>
        <el-table-column label="操作" width="136" fixed="right">
          <template slot-scope="scope">
            <el-row>
              <el-button v-if="scope.row.state == 0" @click="handleClickUpdate(scope.row, '1')" size="mini">已处理</el-button>
              <el-button @click="handleClickUpdate(scope.row, '-1')" size="mini">删除</el-button>
              <!--<el-dropdown size="small">
                <el-button type="primary"><i class="fa fa-list-ul"></i>更多</el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item @click.native="handleClickResetPass(scope.row)">重置密码</el-dropdown-item>
                  <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                  <el-dropdown-item v-has="permissionUpStatus" @click.native="handleClickDisable(scope.row)">{{scope.row.state == 0? '启用' : '禁用'}}</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>-->
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import Crumbs from '@/components/Crumbs';
import * as validators from '@/utils/validators';
import * as message from '@/constants/message';
import { FORMAT_DATE_YYYY_MM_DD } from '@/constants/constants';
import ProvinceCity from '@/components/ProvinceCity';
export default {
  // inject: ['reload'], //注入局部刷新
  data() {
    return {
      searchForm: {
        tel: '',
        state: '',
        adviceType: '',
        date: [],
      },
      permissionEdit: 'SYSTEM_MANAGER_USER_CUSTOMER_LIST_EDIT',
      permissionUpStatus: 'SYSTEM_MANAGER_USER_CUSTOMER_LIST_STATE',

      page: {
        pageNo: 1,
        pageSize: 20,
        pageTotal: 0,
      },
      tableList: [],

    };
  },
  components: {
    Crumbs,
  },
  mounted() {
    this.init();
  },
  activated() {
    if (this.$route.params.customerId) {
      let customerItem = this.tableList.filter((item) => {
        return item.customerId == this.$route.params.customerId;
      });
      let index = this.tableList.indexOf(customerItem[0]);
      for (let item in this.tableList[index]) {
        if (this.$route.params[item]) {
          this.tableList[index][item] = this.$route.params[item];
        }
      }
    }
  },
  methods: {
    init() {
      //this.initDate();
      this.listTable();
      this.getTableHeight();
    },

    formatTime(row, column, cellValue) {
      return this.$Utils.formatDate(cellValue, 'yyyy-MM-dd hh:mm:ss');
    },
    formatAdviceType(row, column, cellValue) {
      if (cellValue == 1) {
        return '建议';
      } else if (cellValue == 2) {
        return '评价';
      } else if (cellValue == 3) {
        return '咨询';
      } else if (cellValue == 4) {
        return '投诉/举报';
      } else if (cellValue == 5) {
        return '其他';
      }
    },
    formatStatue(row, column, cellValue) {
      //-1.删除 0.待处理 1.已处理
      if (cellValue == -1) {
        return '删除';
      } else if (cellValue == 0) {
        return '待处理';
      } else if (cellValue == 1) {
        return '已处理';
      }
    },
    listTable() {
      let data = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        adviceType: this.searchForm.adviceType,
        tel: this.searchForm.tel,
        state: this.searchForm.state,
      };
      this.fetchData(data);
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listTable();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listTable();
    },
    handleClickUpdate(row, state) {
      let txt = state == -1 ? '删除' : '已处理';
      this.$confirm('确定' + txt + '该建议吗？', message.TITLE_PROMPT, {}).then(() => {
        let disableFormData = {
          adviceId: row.adviceId,
          state: state,
        };
        this.$Put(this.$Config.api.advices, disableFormData)
          .then((res) => {
            row.state = state;
            this.$message({
              type: 'success',
              message: message.MSG_ACTION_SUCCESS,
            });
          })
          .catch((error) => {
            if (error.msg) {
              this.$message.error(message.MSG_ACTION_FAIL);
            }
          });
      });
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listTable();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
      //this.initDate();
    },
    fetchData(data) {
      this.$emit('show-loading', true);
      this.$Get(this.$Config.api.advices, data)
        .then((res) => {
          this.tableList = res.data.list;
          this.page.pageTotal = res.data.total;
          this.$emit('show-loading', false);
        })
        .catch((error) => {
          console.error(error);
          this.$emit('show-loading', false);
        });
    },
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
